package com.huike.web.controller.system.dict;

import com.huike.clues.service.ISysDictTypeService;
import com.huike.common.annotation.Log;
import com.huike.common.constant.UserConstants;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictType;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author EthanRuins
 * @date 2023/7/13
 * @apiNote
 */
@RestController
@RequestMapping("/system/dict/type")
@AllArgsConstructor
@Api(tags = "数据字典类型接口")
public class SysDictTypeController extends BaseController {
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping("/{dictId}")
    @ApiOperation(value = "查询字典类型详细")
    public AjaxResult<SysDictType> getById(@PathVariable Long dictId) {
        return AjaxResult.success(dictTypeService.getById(dictId));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询字典类型列表")
    public TableDataInfo<List<SysDictType>> list(SysDictType dictType) {
        startPage();
        List<SysDictType> list = dictTypeService.getList(dictType);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @GetMapping("/export")
    @ApiOperation(value = "导出字典类型数据")
    public AjaxResult<Object> export(SysDictType dictType) {
        List<SysDictType> list = dictTypeService.getList(dictType);
        ExcelUtil<SysDictType> excelUtil = new ExcelUtil<>(SysDictType.class);
        return excelUtil.exportExcel(list, "字典类型");
    }

    @GetMapping("/optionselect")
    @ApiOperation(value = "获取字典选择框列表")
    public AjaxResult<List<SysDictType>> optionselect() {
        List<SysDictType> list = dictTypeService.getDictTypeAll();
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:dict:insert')")
    @PostMapping
    @Log(title = "新增字典类型", businessType = BusinessType.INSERT)
    public AjaxResult<Object> insert(@Validated @RequestBody SysDictType dictType) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictType))) {
            return AjaxResult.error("新增字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.insert(dictType));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:update')")
    @PutMapping
    @Log(title = "修改字典类型", businessType = BusinessType.UPDATE)
    public AjaxResult<Object> update(@Validated @RequestBody SysDictType dictType) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictType))) {
            return AjaxResult.error("修改字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.update(dictType));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:delete')")
    @DeleteMapping("/{dictIds}")
    @Log(title = "删除字典类型", businessType = BusinessType.DELETE)
    public AjaxResult<Object> delete(@PathVariable Long[] dictIds) {
        return toAjax(dictTypeService.delete(dictIds));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:delete')")
    @DeleteMapping("/clearCache")
    @Log(title = "清空缓存", businessType = BusinessType.CLEAN)
    public AjaxResult<Object> clearCache() {
        dictTypeService.clearCache();
        return AjaxResult.success();
    }
}
